package com.ruoyi.controller;

import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.constant.TokenConstants;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.utils.JwtUtils;
import com.ruoyi.common.core.utils.StringUtils;
import io.jsonwebtoken.Claims;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.domain.Evaluate;
import com.ruoyi.service.IEvaluateService;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.page.TableDataInfo;

/**
 * 评价Controller
 * 
 * @author ruoyi
 * @date 2025-03-18
 */
@RestController
@RequestMapping("/evaluate")
public class EvaluateController extends BaseController
{
    @Autowired
    private IEvaluateService evaluateService;

    /**
     * 查询评价列表
     */
    @RequiresPermissions("evaluate:evaluate:list")
    @GetMapping("/list")
    public TableDataInfo list(Evaluate evaluate)
    {
        startPage();
        List<Evaluate> list = evaluateService.selectEvaluateList(evaluate);
        return getDataTable(list);
    }

    /**
     * 导出评价列表
     */
    @RequiresPermissions("evaluate:evaluate:export")
    @Log(title = "评价", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, Evaluate evaluate)
    {
        List<Evaluate> list = evaluateService.selectEvaluateList(evaluate);
        ExcelUtil<Evaluate> util = new ExcelUtil<Evaluate>(Evaluate.class);
        util.exportExcel(response, list, "评价数据");
    }

    /**
     * 获取评价详细信息
     */
    @RequiresPermissions("evaluate:evaluate:query")
    @GetMapping(value = "/{evaluateId}")
    public AjaxResult getInfo(@PathVariable("evaluateId") Long evaluateId)
    {
        return success(evaluateService.selectEvaluateByEvaluateId(evaluateId));
    }

    /**
     * 新增评价
     */
    @RequiresPermissions("evaluate:evaluate:add")
    @Log(title = "评价", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody Evaluate evaluate)
    {
        return toAjax(evaluateService.insertEvaluate(evaluate));
    }

    /**
     * 修改评价
     */
    @RequiresPermissions("evaluate:evaluate:edit")
    @Log(title = "评价", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody Evaluate evaluate)
    {
        return toAjax(evaluateService.updateEvaluate(evaluate));
    }

    /**
     * 删除评价
     */
    @RequiresPermissions("evaluate:evaluate:remove")
    @Log(title = "评价", businessType = BusinessType.DELETE)
	@DeleteMapping("/{evaluateIds}")
    public AjaxResult remove(@PathVariable Long[] evaluateIds)
    {
        return toAjax(evaluateService.deleteEvaluateByEvaluateIds(evaluateIds));
    }

    @PostMapping("/insert")
    public R<?> insert(@RequestBody Evaluate evaluate, @RequestHeader("Authorization") String token) {
        Claims claims = JwtUtils.parseToken(token.replaceFirst(TokenConstants.PREFIX, StringUtils.EMPTY));
        Integer userid = (Integer) claims.get("user_id");
        evaluate.setUserId(Long.valueOf(userid));
        evaluateService.insert(evaluate);
        return R.ok();
    }

    @PostMapping("/selByUidAndCoachId")
    public R<?> selByUidAndCoachId(@RequestHeader("Authorization") String token, @RequestBody Long coachId) {
        Claims claims = JwtUtils.parseToken(token.replaceFirst(TokenConstants.PREFIX, StringUtils.EMPTY));
        Integer userid = (Integer) claims.get("user_id");
        Evaluate evaluate = evaluateService.selByUidAndCoachId(userid, coachId);
        return R.ok(evaluate);
    }
}
